import * as type from './type'

import axios from 'axios'

const state={
    banners:null,
    homelists_p1:[],
    homelists_p2:[],
}       

export const actionCreators = {
    //获取home页面banner数据
    get_banner(){
        return dispatch =>{
            axios({
                url:'/api/getIndexFirstPaintInfo',
                params:{
                    cid:'', 
                    zy_ids:'p8_c4_l4',
                    app_name:'zhe',
                    app_version:'',
                    platform:'',
                    catname:'newest_zhe'                
                }
            }).then(data=>{
                //创建动作
                let action = {
                    type:type.GET_BANNER,
                    payload:data.data.adsInfo.slide_ads.config.slide
                }
                //发送动作
                dispatch( action )

            })
              .catch(error=>console.log(error))
        }
    
    },

    // 获取home页面列表精选专场数据
    get_homelist_p1(val){
        return dispatch => {
            axios({
                url:'/api/getIndexNavSkip',
                params:{
                    page:val,
                    zy_ids: 'p8_c4_l4',
                    app_name: 'zhe',
                    catname: 'tab_hpzc',
                    flag: 'tab_hpzc',
                }
            }).then(data =>{
                let action = {
                    type:type.GET_HOMELIST_P1,
                    payload: data.data.GoodsRes.goods
                }
                dispatch(action)
            })
              .catch(error => console.log(error))
        }
    },

    //获取home页面列表精选单品数据
    get_homelist_p2(val){
        return dispatch => {
            axios({
                url:'/api/getGoods',
                params:{
                    page:val,
                    zy_ids: 'p8_c4_l4_0',
                    app_name: 'zhe',
                    catname:' tab_hpdp',
                    flag:'tab_hpdp',
                }
            }).then(data =>{
                console.log('p2',data.data.data.goods)
                let action = {
                    type:type.GET_HOMELIST_P2,
                    payload: data.data.data.goods
                }
                dispatch(action)
            })
              .catch(error => console.log(error))
        }
    }
}

export const reducer = ( prevState = state , action ) => {
    //复制一层数据
    const  newState = {
        ...prevState
    }
    switch ( action.type ) {        
        case type.GET_BANNER:
            newState.banners = action.payload
            break;

        case type.GET_HOMELIST_P1:          
            newState.homelists_p1 = action.payload;
            break;

        case type.GET_HOMELIST_P2:          
            newState.homelists_p2 = action.payload;
            break;

        default:
            break;
    }
    return newState
}